<template>
  <div class="dashboard-container">
    <div class="filter-container">
      <el-row :gutter="10" style="width:100%">
        <el-col :span="4">
          <el-input
            v-model="listQuery.username"
            placeholder="用户昵称"
            class="input-with-select"
            @keypress.enter.native="reloadList"
          />
        </el-col>
        <el-col :span="4">
          <el-date-picker
            v-model="listQuery.created"
            style="width: 100%;"
            type="datetimerange"
            range-separator="~"
            value-format="yyyy-MM-dd HH:mm:ss"
            start-placeholder="创建开始日期"
            end-placeholder="创建结束日期"
            :default-time="['00:00:00', '23:59:59']"
          />
        </el-col>
        <el-col :span="6">
          <div>
            <el-button
              class="filter-item"
              type="primary"
              icon="el-icon-search"
              @click="reloadList"
            >搜索</el-button>
            <!-- <el-button
              class="filter-item"
              style="margin-left: 10px;"
              icon="el-icon-edit"
              @click="addNews"
            >
              添加
            </el-button> -->
            <el-button
              class="filter-item"
              type="primary"

              style="margin-left: 10px;"
              icon="el-icon-document-add"
              @click="addSX"
            >
              添加授信
            </el-button>
          </div>

        </el-col>
      </el-row>
    </div>
    <div style="padding:0 20px">

      <el-table
        ref="multipleTable"
        v-loading="listLoading"
        :data="list"
        height="calc(100vh - 200px)"
        style="width: 100%;"
        size="mini"
        border
        tooltip-effect="dark"
        :header-cell-style="{ background: '#eaf1fd', color: '#333' }"
      >
        <el-table-column
          label="用户昵称"
          prop="username"
          width="160"
        />
        <el-table-column
          label="账号"
          prop="context"
          width="160"
        >
          <template slot-scope="{ row }">
            <div>
              <span style="margin-right:5px">+{{ row.area_code }}</span>
              <span>{{ row.mobile }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="余额"
          width="100"

          prop="money"
        />
        <el-table-column
          label="授信额度"
          width="100"
          prop="credit_limit"
        />
        <el-table-column
          label="已使用授信额度"
          width="120"
          prop="credit_use"
        />
        <el-table-column
          label="授信余额"
          width="100"
        >
          <template slot-scope="{ row }">
            <div>
              <span>{{ (Number(row.credit_limit) - Number(row.credit_use)).toFixed(2) }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="状态"
          width="100"
        >
          <template slot-scope="{ row }">
            <div>
              <span v-if="row.status == 0" style="color:red">禁用</span>
              <span v-else style="color:blue">启用</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          prop="add_time"
          width="160"
        />
        <el-table-column
          label="更新时间"
          prop="update_time"
          width="160"
        />
        <el-table-column
          label="操作"
          align="right"
          width="350"
          class-name="small-padding fixed-width"
          fixed="right"
        >
          <template slot-scope="{ row }">
            <el-button
              size="mini"
              type="primary"
              @click="edit(row)"
            >
              编辑
            </el-button>
            <el-button
              size="mini"
              type="primary"
              @click="addSX(row)"
            >
              添加授信
            </el-button>
            <el-button
              size="mini"
              type="primary"
              @click="payBack(row)"
            >
              授信还款
            </el-button>

            <el-button
              size="mini"
              type="primary"
              @click="showSecondPassWord(row)"
            >
              设置安全密码
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
    </div>
    <el-dialog
      :title="'【'+ row.username +'】授信还款'"
      :visible.sync="showPayBack"
      width="400px"
      :before-close="handleClose"
    >
      <div style="display:flex;padding:20px; align-items:center">
        <div style="width:80px">还款金额:</div>
        <el-input v-model="money" placeholder="请输入还款金额" />
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="showPayBack = false">取 消</el-button>
        <el-button type="primary" @click="submitPay()">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      :title="'修改【'+ row.username +'】密码'"
      :visible.sync="dialogVisible"
      width="400px"
      :before-close="handleClose"
    >
      <div style="display:flex;padding:20px; align-items:center">
        <div style="width:60px">新密码:</div>
        <el-input v-model="newpassword" placeholder="请输入新密码" show-password />
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitPass()">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      :title="'设置【'+ row.username +'】安全密码'"
      :visible.sync="showSecondPass"
      width="400px"
      :before-close="handleClose"
    >
      <div style="display:flex;padding:20px; align-items:center">
        <div style="width:80px">安全密码:</div>
        <el-input v-model="secondPassWord" placeholder="请输入安全密码" show-password />
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="showSecondPass = false">取 消</el-button>
        <el-button type="primary" @click="submitSPass()">确 定</el-button>
      </span>
    </el-dialog>
    <Detial :uid="uid" :detial="detial" :status="status" :member-list="memberList" :visible="visibleSX" @update="closeDraw" @reload="reloadList" />
    <UserDetial :visible="visible" :detial="detial" :status="status" @update="closeDraw" @reload="reloadList" />
  </div>
</template>

<script>
import { listUser, setSafePassword, repayment } from '@/api/user/index'

import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import Detial from '../finance/components/creditDetial.vue'
import UserDetial from './components/Detial.vue'

export default {
  name: 'Dashboard',
  components: {
    Pagination,
    Detial,
    UserDetial
  },
  data() {
    return {
      dialogVisible: false,
      newpassword: '',
      data_arr: [],
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        username: '',
        created: []
      },
      visible: false,
      status: 1,
      id: '',
      detial: {},
      message: '',
      row: '',
      showSecondPass: false,
      secondPassWord: '',
      showPayBack: false,
      money: '',
      visibleSX: false,
      memberList: [],
      uid: ''
    }
  },
  computed: {
  },
  created() {
    this.getList()
    this.getMemberList()
  },
  methods: {
    getMemberList() {
      listUser({
        status: 1,
        limit: 9999999
      }).then(res => {
        this.memberList = res.data.data
      })
    },
    addSX(row) {
      if (row) {
        this.uid = row.id
      }
      this.status = 1
      this.visibleSX = true
    },
    submitPay() {
      if (!this.money) return this.$message.error('请输入还款金额')
      repayment({
        money: this.money,
        user_id: this.row.id
      }).then(response => {
        this.$message.success(response.msg)
        this.showPayBack = false
        this.getList()
      })
    },
    payBack(row) {
      this.row = row
      this.showPayBack = true
    },
    handleClose() {
      this.dialogVisible = false
      this.showSecondPass = false
      this.showPayBack = false
      this.visibleSX = false
    },
    showSecondPassWord(row) {
      this.secondPassWord = ''
      this.row = row
      this.showSecondPass = true
    },
    submitPass() {
      listUser(this.listQuery).then(response => {
        this.$message.success('修改成功')
        this.dialogVisible = false
        this.getList()
      })
    },
    submitSPass() {
      setSafePassword({
        'user_id': this.row.id,
        'password': this.secondPassWord
      }).then(response => {
        this.$message.success('修改成功')
        this.showSecondPass = false
        this.getList()
      })
    },
    showPass(row) {
      this.dialogVisible = true
      this.newpassword = ''
      this.row = row
    },
    closeDraw() {
      this.visible = false
      this.visibleSX = false
    },
    addNews() {
      this.status = 1
      this.visible = true
    },
    edit(row) {
      this.detial = { ...row }
      this.status = 2
      this.visible = true
    },
    // delet(row) {
    //   this.$confirm('确认删除该航班, 是否继续?', '提示', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   }).then(() => {
    //     delFlight({
    //       id: row.id
    //     }).then(res => {
    //       this.$message({
    //         type: 'success',
    //         message: '删除成功!'
    //       })
    //       this.getList()
    //     })
    //   }).catch(() => {

    //   })
    // },
    reloadList() {
      this.listQuery.page = 1
      this.visible = false
      this.visibleSX = false
      this.getList()
    },
    getList() {
      this.listLoading = true
      listUser(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    }
  }
}
</script>
<style scoped>
.filter-container{
  padding:20px 20px 10px;

}

.upload-demo{
  margin: 20px 0 10px;
}
</style>
